package com.szqhyxj.youxiangju.biz.partnerHome.home.peasonalperformancerepayment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.partnerHome.home.merchantdetails.MerchantDetailsActivity;
import com.szqhyxj.youxiangju.biz.partnerHome.home.merchantdetails.MerchantDetailsContract;
import com.szqhyxj.youxiangju.biz.partnerHome.home.partnerdetails.PartnerDetailsActivity;
import com.szqhyxj.youxiangju.biz.partnerHome.home.teamperformancerepayment.RefreshAdapter;
import com.szqhyxj.youxiangju.biz.partnerHome.home.RefreshItemDecoration;
import com.szqhyxj.youxiangju.biz.partnerHome.home.teamperformancerepayment.RowsItem;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by leung on 2018/11/8.
 */

public class PeasonalPerformanceRepaymentActivity extends BaseActivity implements NavigationBar.NavigationBarListener, PeasonalPerformanceRepaymentContract.View, PeasonalAdapter.ItemClickListener {

    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;
    @BindView(R.id.peasonalperformancedata)
    RecyclerView peasonalperformancedata;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    PeasonalAdapter adapter;
    private LinearLayoutManager mLinearLayoutManager;

    PeasonalPerformanceRepaymentPresenter presenter;

    public static void start(Activity activity) {
        Intent intent = new Intent();
        intent.setClass(activity, PeasonalPerformanceRepaymentActivity.class);
        activity.startActivity(intent);

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_peasonal_performance_repayment);
        ButterKnife.bind(this);
        navigationBar.setNavigationBarListener(this);
        presenter = new PeasonalPerformanceRepaymentPresenter(this);
        presenter.init(1, 1, 10, 1);
        initListener();
        swipeLayout.setEnabled(false);
    }

    @Override
    public void navigationLeft() {
        finish();
    }

    @Override
    public void navigationRight() {

    }

    @Override
    public void showData(List<PeasonalRowsItem> list) {
        adapter = new PeasonalAdapter(this, list, this);
        mLinearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);


        //添加动画
        peasonalperformancedata.setItemAnimator(new DefaultItemAnimator());

        //添加分割线
        peasonalperformancedata.addItemDecoration(new RefreshItemDecoration(this, RefreshItemDecoration.VERTICAL_LIST));

        peasonalperformancedata.setLayoutManager(mLinearLayoutManager);
        peasonalperformancedata.setAdapter(adapter);
    }

    private void initListener() {

        initLoadMoreListener();

    }


    private void initLoadMoreListener() {

        peasonalperformancedata.setOnScrollListener(new RecyclerView.OnScrollListener() {
            int lastVisibleItem;

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                //判断RecyclerView的状态 是空闲时，同时，是最后一个可见的ITEM时才加载
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == adapter.getItemCount()) {

                    //设置正在加载更多
                    adapter.changeMoreStatus(adapter.LOADING_MORE);

                    //改为网络请求
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {

                            presenter.load();

                        }
                    }, 3000);


                }

            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                //最后一个可见的ITEM
                lastVisibleItem = layoutManager.findLastVisibleItemPosition();
            }
        });

    }


    @Override
    public void showLoad(List<PeasonalRowsItem> list) {
        List<RowsItem> footerDatas = new ArrayList<RowsItem>();

        adapter.AddFooterItem(list);
        //设置回到上拉加载更多
        adapter.changeMoreStatus(adapter.PULLUP_LOAD_MORE);
        //没有加载更多了
        //mRefreshAdapter.changeMoreStatus(mRefreshAdapter.NO_LOAD_MORE);
        // Toast.makeText(TeamPerformanceRepaymentActivity.this, "更新了 " + footerDatas.size() + " 条目数据", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showNodata() {
        adapter.changeMoreStatus(adapter.NO_LOAD_MORE);
    }

    @OnClick(R.id.search)
    public void onClick() {
        //   presenter.search(searchName.getText().toString());
    }

    @Override
    public void onClickItem(String organId) {
        MerchantDetailsActivity.start(this, organId, 1, 1);
    }
}
